package zx.x.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import zx.x.modules.sys.entity.SysDistrictEntity;
import zx.x.modules.sys.service.SysDistrictService;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;



/**
 * 省市区
 *
 * @author x
 * @email x
 * @date 2022-03-03 14:29:53
 */
@RestController
@RequestMapping("/sysdistrict")
public class SysDistrictController {
    @Autowired
    private SysDistrictService sysDistrictService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("x:sysdistrict:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDistrictService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("x:sysdistrict:info")
    public R info(@PathVariable("id") Integer id){
        SysDistrictEntity sysDistrict = sysDistrictService.getById(id);

        return R.ok().put("sysDistrict", sysDistrict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("x:sysdistrict:save")
    public R save(@RequestBody SysDistrictEntity sysDistrict){
        sysDistrictService.save(sysDistrict);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("x:sysdistrict:update")
    public R update(@RequestBody SysDistrictEntity sysDistrict){
        sysDistrictService.updateById(sysDistrict);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("x:sysdistrict:delete")
    public R delete(@RequestBody Integer[] ids){
        sysDistrictService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation(value = "获取省级列表")
    @PostMapping(value = "/get/parent/list")
    public R getParentList (String name) {
        return R.ok().put("list",sysDistrictService.getParentList(name));
    }


    @ApiOperation(value = "根据父级id获取子集列表")
    @ApiImplicitParam(name = "parentId", value = "省级的id", required = true)
    @PostMapping(value = "/get/child/list")
    public R getChildList (Integer parentId ,String name) {
        return R.ok().put("list",sysDistrictService.getChildList(parentId,name));
    }
}
